﻿using log4net;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using Timesheet.Data.Infrastructure;
using Timesheet.Data.Repository;
using Timesheet.Domain;

namespace Timesheet.Service
{
    public class TeamService : ITeamService
    {
        private readonly ITeamRepository _Repository;
        private readonly IUnitOfWork _UnitOfWork;         
        private readonly ITeamDetailRepository _TeamDetailRepository;

        public ILog Logger { get; private set; }

        public TeamService(ITeamRepository TeamRepository,ITeamDetailRepository teamDetailRepository, IUnitOfWork unitOfWork)
        {
            this._Repository = TeamRepository;
            this._UnitOfWork = unitOfWork;
            _TeamDetailRepository = teamDetailRepository; 
        }


        #region ITeamService Members

        public IList<Team> GetTeams()
        {
            return this._Repository.GetAll().ToList();
        }


        public IList<Team> GetOrdersTeams()
        {
            return this._Repository.GetAll().OrderBy(p=>p.TeamName).ToList();
        }

        public IList<Team> GetOrdersTeams(long prjId)
        {
            return _Repository.GetQuery().Where(x => x.ProjectID == prjId).OrderBy(p => p.TeamName).ToList();
        }

        public bool CheckExist(Team entity)
        {
            throw new NotImplementedException();
        }

        public Team GetByID(Int64 id)
        {
            return this._Repository.GetById(id);
        }

        public void Add(Team entity)
        {
            this._Repository.Add(entity);
        }

        public void Update(Team entity)
        {
            this._Repository.Update(entity);
        }
        public void SimpleUpdate(Team entity)
        {
            Team domain = _Repository.GetById(entity.Id);
            domain.Active = entity.Active;
            domain.DesciplineID = entity.DesciplineID;
            domain.LocationID = entity.LocationID;
            domain.TeamName = entity.TeamName;            

            _Repository.Update(domain);
        }
        public void Delete(Team entity)
        {
            this._Repository.Delete(entity);
        }  
        
        public IList<Team> GetTeams(long prjId)
        {
            return _Repository.GetQuery().Where(x => x.ProjectID == prjId).ToList();
        }
      
        public Team GetTeamByUSers(long userID)
        {
           var qTeamDetail = _TeamDetailRepository.GetQuery().Where(x=>x.EmployerID==userID);
           var qTeam = _Repository.GetQuery().Where(x => x.Active && qTeamDetail.Any(t=>t.TeamID ==x.Id));
           return qTeam.FirstOrDefault();
        }

        #endregion




        public IList<Team> GetRefTeams(long idProject, User user)
        {
            if (user.Coordinator == true || user.Manager == true)
            {
                return _Repository.GetQuery().Where(x => x.ProjectID == idProject).ToList();
            }
            else
            {
                IList<long> teamIds = _TeamDetailRepository.GetQuery().Where(x => x.EmployerID == user.Id).Select(x => x.TeamID).ToList();
                return _Repository.GetQuery().Where(x => x.ProjectID == idProject && teamIds.Contains(x.Id)).Distinct().ToList();
            }
            
        }
    }
}
